package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.JianzhengrenAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.SearchUserTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class SearchTransFlowerListActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener,JianzhengrenAdapter.OnAdapterClickListener {


    private PullToRefreshListView searchChibanPullList;
    private Context mContext;
    private JianzhengrenAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private int searchTaskId = -1;
    private String keywords;
    private TextView back;
    private NoDataUtil nodataUtil;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_trans_flower_list);
        mContext = this;
        initView();
        View view = findViewById(R.id.content_layout);
        nodataUtil = new NoDataUtil(mContext,view);
    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null){
            keywords = intent.getStringExtra("keyword");
        }
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchTransFlowerListActivity.this.finish();
            }
        });
        searchChibanPullList = (PullToRefreshListView) findViewById(R.id.search_chiban_pull_list);
        mAdapter = new JianzhengrenAdapter(mContext);
        mAdapter.setOnAdapterClickListener(this);
        searchChibanPullList.init(mContext,true);
        searchChibanPullList.setAdapter(mAdapter,true);
        searchChibanPullList.setOnRefreshListener(this);
        searchChibanPullList.setOnLoadDataListener(this);
        searchChibanPullList.doRefresh();
    }

    private void getPaiHaiBang() {
        if(searchTaskId != -1){
            return;
        }
        if(currentPage == 0){
            searchChibanPullList.setNoNextPage();
            return;
        }

        SearchUserTask task = new SearchUserTask(keywords,size, currentPage);
        task.addListener(anchorListListener);
        searchTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener anchorListListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == searchTaskId){
                searchTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    searchChibanPullList.onRefreshComplete();
                    searchChibanPullList.enableLoadMore();
                }else{
                    searchChibanPullList.onLoadDataComplete(true);
                }
                List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
                if(data != null){
                    fansList = (List<EatShowUserInfo>) data;
                }

                if(fansList != null && fansList.size()>0){
                    mAdapter.updateData(fansList);
                }

                currentPage = result;

                if(mAdapter != null && mAdapter.getCount() == 0){
                    nodataUtil.ListViewEmpty(searchChibanPullList,null,null,-1);
                }else{
                    nodataUtil.ListViewNoEmpty(searchChibanPullList);
                }

            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == searchTaskId){
                searchTaskId = -1;
                if(currentPage == 1){
                    searchChibanPullList.onRefreshComplete();
                }else{
                    searchChibanPullList.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

    };

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getPaiHaiBang();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getPaiHaiBang();
    }

    @Override
    public void OnAdapterClick(EatShowUserInfo info) {
        if (info != null) {
            Intent intent = new Intent(mContext, TransFlowActivity.class);
            intent.putExtra("USERID", info.getUserId());
            startActivity(intent);
        }

    }
}
